package com.lc.hongtai.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonReg;
import com.lc.hongtai.toast.LovelyToast;
import com.lc.hongtai.view.ClearEditText;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by Administrator on 2017/5/31.
 */
public class RegisterNextActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.et_01)
    private ClearEditText et_01;
    @BoundView(R.id.et_03)
    private ClearEditText et_03;

    @BoundView(R.id.bt_registered)
    private Button bt_registered;
    String reg = "^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$";
    boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_next);
        initLinstener();
        flag = et_01.getText().toString().trim().matches(reg);
    }


    public void initLinstener() {
        bt_registered.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_registered:


           /*     if (TextUtils.isEmpty(et_01.getText().toString().trim())){
                    LovelyToast.makeText(RegisterNextActivity.this, "用户姓名不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }else if (!flag ){

                    LovelyToast.makeText(RegisterNextActivity.this, "请输入真实姓名", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

                else if (TextUtils.isEmpty(et_03.getText().toString().trim())){
                    LovelyToast.makeText(RegisterNextActivity.this, "身份证号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }else {
                    getAccessToken();
                }*/
                getAccessToken();



               /* Intent intent = new Intent();
                intent.putExtra("url", "http://180.168.26.114:20010/p2p-deposit/gateway.htm");
                intent.setClass(RegisterNextActivity.this, RegistWebActivity_return.class);

                startActivity(intent);*/
                break;
        }
    }

    private void getAccessToken() {
        new JsonAccessToken("common/reg", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                initReg(info.access_token);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                //UtilToast.show(RegisterActivity.this, "安全验证失败！");
                LovelyToast.makeText(RegisterNextActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                dismissLoading();
            }
        }).execute(this, true);

    }

    private void initReg(String access_token) {

        new JsonReg(access_token, getIntent().getStringExtra("phone"), getIntent().getStringExtra("password"), getIntent().getStringExtra("recommender"), et_01.getText().toString().trim(), et_03.getText().toString().trim(), new AsyCallBack<JsonReg.RegistBackInfo>() {
            @Override
            public void onSuccess(String toast, int type, JsonReg.RegistBackInfo info) throws Exception {

                //   MyApplication.myPreferences.saveUid(info.uid);
//                finish();
                if (info.status.equals("1")) {

                    MyApplication.myPreferences.saveUid(info.uid);
                    Intent intent = new Intent();
                    intent.putExtra("url", info.url);
                    intent.setClass(RegisterNextActivity.this, RegistWebActivity_return.class);
                    Log.d(info.uid, "23333333333333333");
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                // UtilToast.show(RegisterActivity.this, JsonReg.TOAST);
                LovelyToast.makeText(RegisterNextActivity.this, JsonReg.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

        }).execute(this, true);
    }
}
